<template>
  <el-container >
    <el-header>
      <el-row>
        <el-col :span="4"
          ><div class="grid-content bg-purple">logo</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">后台管理系统</div></el-col
        >
        <el-col :span="4">
          <el-dropdown @command="exitLogin">
            <span class="el-dropdown-link">
              {{ this.userinfo.username }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
        unique-opened
        router
        >
          <el-submenu
            :index="item.path"
            v-for="(item, key) in userList"
            :key="key"
          >
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="value.path"
              v-for="(value, keys) in item.children"
              :key="keys"
              >{{value.authName}}</el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
          <!-- 嵌套路由 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      userinfo: {},
      userList: [],
    };
  },
  methods: {
    getUserinfo() {
      this.$store.commit("saveUserInfo");
      this.userinfo = this.$store.state.userinfo;
    },
    exitLogin(command) {
      if (command === "a") {
        localStorage.removeItem("userinfo");
        console.log(localStorage.getItem("userinfo"));
        this.$router.push("/login");
      }
    },
    getAside() {
      this.$req.get("/menus").then((res) => {
        let {
          data: { data, meta },
        } = res;
        if (meta.status === 200) {
          this.userList = data;
          // console.log(data);
        }
      });
    },
  },
  mounted() {
    this.getUserinfo();
    this.getAside();
  },
};
</script>

<style>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>